<?php

namespace app\admin\controller;

use app\admin\model\Agent as AgentModel;
use app\admin\model\SmsList as SmsListModel;
use app\admin\model\SmsSendList as SmsSendListModel;
use app\admin\validate\Agent as AgentValidate;
use app\admin\validate\Sms;
use app\common\model\Export;

class Agent extends Controller
{
    protected function initialize()
    {
        parent::initialize();
    }

    /**
     * @api {GET} /admin/agent/list 代理商列表
     */
    public function list()
    {
        $model = new AgentModel();
        $data = input('post.');

        return $model->getList($data);
    }

    /**
     * @api {POST} /admin/agent/create 代理商新增
     */
    public function create()
    {
        $data = input('post.');
        $validate = new AgentValidate();
        if (!$validate->scene('create')->check($data)) {
            return fail($validate->getError());
        }
        if (!empty($data['mobile_service']) && !$validate->scene('mobile_service')->check($data)) {
            return fail($validate->getError());
        }

        $model = new AgentModel();

        return $model->create($data);
    }

    /**
     * @api {POST} /admin/agent/edit 代理商修改
     */
    public function edit()
    {
        $data = input('post.');
        $validate = new AgentValidate();
        if (!$validate->scene('edit')->check($data)) {
            return fail($validate->getError());
        }
        if (!empty($data['mobile_service']) && !$validate->scene('mobile_service')->check($data)) {
            return fail($validate->getError());
        }

        $model = new AgentModel();

        return $model->edit($data);
    }

    /**
     * 充值
     */
    public function recharge()
    {
        $data = input('post.');
        $validate = new AgentValidate();
        if (!$validate->scene('recharge')->check($data)) {
            return fail($validate->getError());
        }

        $model = new AgentModel();

        return $model->recharge($data);
    }

    /**
     * 临下收款码收款
     */
    public function queryDmjOrderByTradeNo()
    {
        $data = input('post.');
        $validate = new AgentValidate();
        if (!$validate->scene('queryDmjOrderByTradeNo')->check($data)) {
            return fail($validate->getError());
        }

        $model = new AgentModel();

        return $model->queryDmjOrderByTradeNo($data);
    }

    /**
     * 资金明细.
     */
    public function balanceList()
    {
        $model = new AgentModel();
        $data = input('post.');

        return $model->getBalanceList($data);
    }

    /**
     * 统计资金.
     */
    public function balanceCount()
    {
        $model = new AgentModel();
        $data = input('post.');

        return $model->getBalanceCount($data);
    }

    /**
     * 导出资金明细.
     */
    public function exportBalanceExcel()
    {
        $model = new AgentModel();
        $data = input('post.');
        $data['is_export'] = 1;
        $list = $model->getBalanceList($data);
        $export = new Export();

        return $export->doExport($list, 2);
    }

    /**
     * 删除.
     */
    public function delete()
    {
        $data = input('post.');
        $model = new AgentModel();

        return $model->delete($data);
    }

    /**
     * 话费供应商.
     */
    public function mobileSupplierList()
    {
        $model = new AgentModel();

        return $model->mobileSupplierList();
    }

    /**
     * 指定代理商已经关联的话费供应商 id 集合.
     */
    public function associationMobileSupplierIds()
    {
        $data = input('post.');
        $model = new AgentModel();

        return $model->associationMobileSupplierIds($data);
    }

    //短信通知设置
    public function smsList()
    {
        $data = input('post.');
        $model = new SmsListModel();

        return $model->smsList($data);
    }

    //添加短信通知
    public function addSms()
    {
        $data = input('post.');
        $validate = new Sms();
        if (!$validate->scene('add_sms')->check($data)) {
            return fail($validate->getError());
        }
        $model = new SmsListModel();

        return $model->addSms($data);
    }

    //删除短信通知
    public function delSms()
    {
        $data = input('post.');
        $model = new SmsListModel();

        return $model->delSms($data);
    }

    //短信通知详情
    public function smsDetail()
    {
        $data = input('post.');
        $model = new SmsSendListModel();

        return $model->getList($data);
    }

    //发送短信
    public function sendSms()
    {
        $data = input('post.');
        $model = new SmsListModel();

        return $model->sendSms($data);
    }

    /**
     * 代理商关联的供应商列表
     */
    public function relatedMobileSupplierList()
    {
        $data = input('post.');
        $model = new AgentModel();

        return $model->relatedMobileSupplierList($data);
    }

    /**
     * 保存代理商话费轮询配置
     */
    public function updateMobilePolling()
    {
        $data = input('post.');
        $model = new AgentModel();

        return $model->updateMobilePolling($data);
    }
}
